using FytSoa.Domain.Erp;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Common.Param;
using FytSoa.Common.Result;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Erp;

/// <summary>
/// erp-销售表服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v9")]
public class ErpSalesOrderService : IApplicationService 
{
    private readonly SugarRepository<ErpSalesOrder> _thisRepository;
    private readonly SugarRepository<ErpSalesOrderItem> _itemRepository;
    public ErpSalesOrderService(SugarRepository<ErpSalesOrder> thisRepository
    ,SugarRepository<ErpSalesOrderItem> itemRepository)
    {
        _thisRepository = thisRepository;
        _itemRepository = itemRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<ErpSalesOrderDto>> GetPagesAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(param.Key),m=>m.Number.Contains(param.Key))
            .Includes(m=>m.Customer)
            .Includes(m=>m.Employee)
            .OrderByDescending(m=>m.Id)
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<ErpSalesOrderDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<ErpSalesOrderDto> GetAsync(long id)
    {
        var model = await _thisRepository.AsQueryable().Includes(m => m.Customer)
            .Includes(m => m.Employee)
            .FirstAsync(m => m.Id == id);
        var res=model.Adapt<ErpSalesOrderDto>();
        var items = await _itemRepository.AsQueryable().Includes(m=>m.Product,m=>m.Unit).Where(m => m.OrderId == id)
            .ToListAsync();
        res.List = items.Adapt<List<ErpSalesOrderItemDto>>();
        return res;
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [UnitOfWork]
    public async Task AddAsync(ErpSalesOrderDto model)
    {
        var toDay = await _thisRepository.CountAsync(m=>m.CreateTime.ToString("yyyy-MM")==DateTime.Now.ToString("yyyy-MM"));
        model.Number = "SA".ResNumber(toDay, "yyyyMM");
        var orderId=await _thisRepository.InsertReturnSnowflakeIdAsync(model.Adapt<ErpSalesOrder>());
        foreach (var item in model.List)
        {
            item.OrderId = orderId;
        }

        await _itemRepository.InsertRangeAsync(model.List.Adapt<List<ErpSalesOrderItem>>());
    }


    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [UnitOfWork]
    public async Task ModifyAsync(ErpSalesOrderDto model)
    {
        await _thisRepository.UpdateAsync(model.Adapt<ErpSalesOrder>());
        await _itemRepository.DeleteAsync(m => m.OrderId == model.Id);
        foreach (var item in model.List)
        {
            item.OrderId = model.Id;
        }
        await _itemRepository.InsertRangeAsync(model.List.Adapt<List<ErpSalesOrderItem>>());
    }

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete,UnitOfWork]
    public async Task<bool> DeleteAsync([FromBody] List<long> ids)
    {
        await _itemRepository.DeleteAsync(m => ids.Contains(m.OrderId));
        return await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
    }
}
